package com.dsz.hse.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import web.models.CommonResult;
import web.models.Page;

import com.dsz.hse.dao.IRecordDao;
import com.dsz.hse.model.SecurityCheckRecord;
import com.dsz.hse.model.User;

/**
 * 值班记录 MVC Controller。
 * 
 * @author crjun
 */
@Controller
@RequestMapping("record")
public class RecordController {
	@Resource
	private IRecordDao recordDao;
	
	public void setRecordDao(IRecordDao recordDao) {
		this.recordDao = recordDao;
	}

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult addRecord(@ModelAttribute SecurityCheckRecord record, HttpServletRequest request) {
//    	dutyRecord.setDutyMan(((User)(request.getSession().getAttribute("currentUser"))).getUsername());
//    	dutyRecord.setDutyManFullName(((User)(request.getSession().getAttribute("currentUser"))).getFullName());
    	recordDao.addRecord(record);
        return new CommonResult(new Integer(1));
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateRecord(@ModelAttribute SecurityCheckRecord dutyRecord) {
    	recordDao.removeRecord(dutyRecord.getId());
    	recordDao.addRecord(dutyRecord);
        return new CommonResult(new Integer(1));
    }

    @RequestMapping(value = "/remove/{recordId}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult removeRecord(@PathVariable int recordId) {
    	recordDao.removeRecord(recordId);
        return new CommonResult(new Integer(1));
    }

    @RequestMapping("/query")
    @ResponseBody
    public Page<SecurityCheckRecord> queryRecords(@RequestParam String filter, @RequestParam int page, @RequestParam int rows) {
        List<SecurityCheckRecord> dutyRecords = recordDao.getPageDutyRecords(filter, page, rows);
        int count = recordDao.getRecordsCount(filter);
        return new Page<SecurityCheckRecord>(count, dutyRecords);
    }

    @RequestMapping("/queryone/{recordId}")
    @ResponseBody
    public SecurityCheckRecord queryRecord(@PathVariable String recordId) {
    	String filter = "id=" + recordId;
        List<SecurityCheckRecord> records = recordDao.getPageDutyRecords(filter, 1, 1);
        if (null != records)
        	return records.get(0);
        return null;
    }
}
